<?php

/*
 * Funkce přidává do databáze relace mezi rolemi a akcemi - vrací true/false
 */
function mailcontent_db_add_role_actions($data){
      global $user;
    if (user_access('Settings - action')) {
    if (drupal_write_record('mailcontent_role_actions', $data)){
        return true;
    } else {
        return false;
    }
        } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} // OK

/*
 * Funkce přidává do databáze mailbox - vrací true/false
 */
function mailcontent_db_login_add($name, $pass, $server, $timeout) {
      global $user;
    if (user_access('settings accounts')) {


    global $user;
    $data = array(
            'duid' => $user->uid,
            'username' => $name,
            'pass' => $pass,
            'server' => $server,
            'timeout' => $timeout
    );
    if (drupal_write_record('mailcontent_login', $data)) {
        return true;
    }
    return false;
        } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK

/*
 * Funkce přidává do databáze novou akci - vrací true/false
 */
function mailcontent_db_action_add($action, $desc, $path) {

    global $user;
    if (user_access('settings actions')) {

    $data = array(
            'name' => $action,
            'description' => $desc,
            'path' => $path
    );
    if (drupal_write_record('mailcontent_actions', $data)) {
        return true;
    }
        } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return false;
} //OK

/*
 * Funkce přidává relaci mezi maily a uživateli drupalu - vrací true/false
 */
function mailcontent_db_subscribers_add($idacc, $mailaddress, $role) { //TODO spojit primo s formularem
    global $user;
    if (user_access('content edit')) {
    if (db_result(db_query("SELECT * FROM {mailcontent_subscribers} WHERE duid=".$user->uid." and mailaddress='".$mailaddress."' and idacc='all'"))) {
        mailcontent_info(t("This mail address(".$mailaddress.") have rule for all accounts! - you can not add another one.."));
        return false;
    }
    if (db_result(db_query("SELECT * FROM {mailcontent_subscribers} WHERE duid=".$user->uid." and mailaddress='".$mailaddress."' and idacc='".$idacc."'"))) {
        mailcontent_info(t("For this mail address (".$mailaddress.") already exits rule!"));
        return false;
    }
    if ($idacc == 'all') {
        if (db_result(db_query("SELECT * FROM {mailcontent_subscribers} WHERE idacc != 'all' and mailaddress='".$mailaddress."' and idr='".$role."' and duid=".$user->uid))) {
            db_query("DELETE FROM {mailcontent_subscribers} WHERE idacc != 'all' and mailaddress='".$mailaddress."' and duid=".$user->uid);
            drupal_set_message(t('Unnecessary reccords was deleted..'));
        }
    }
    $data = array(
            'duid' => $user->uid,
            'mailaddress' => $mailaddress,
            'idacc' => $idacc,
            'idr' => $role
    );
    if (drupal_write_record('mailcontent_subscribers', $data)) {
        return true;
    }
        } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return false;
} //OK

/*
 * Funkce přidává do databáze nový záznam o mailu - vrací true/false
 */
function mailcontent_db_add_mail($data) {
    global $user;
    $data['duid'] = $user->uid;
    $data['state'] = 0;
    $data['idn'] = 0;
//    db_query("INSERT INTO {mailcontent_mail}(`body`, `type`, `from`, `received`, `dateexp`, `copy`, `to`, `synchronization`, `duid`, `username`, `state`, `idn`)        VALUES('".$data['body']."', '".$data['type']."', '".$data['from']."', '".$data['received']."', '".$data['dateexp']."', '".$data['copy']."', '".$data['to']."', '".$data['synchronization']."', ".$data['duid'].", '".$data['username']."', ".$data['state'].", ".$data['idn'].")")
    if (drupal_write_record('mailcontent_mail', $data)) {
        return db_last_insert_id("mailcontent_mail", "idm");
    } else {
        mailcontent_error(t('Error in writing to database - adding new mail!'));
        return FALSE;
    }
}  //OK

/*
 * Funkce přidává do databáze nový záznam o bloku mailu- vrací true/false
 */
function mailcontent_db_add_content($node){
    $data['idm'] = $node['idm'];
    $data['weight_content'] = 0;

    if ($node['title']){
        $data['value_content'] = $node['title'];
        $data['type_content'] = 'title';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['teaser']){
        $data['value_content'] = $node['teaser'];
        $data['type_content'] = 'teaser';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['body']){
        $data['value_content'] = $node['body'];
        $data['type_content'] = 'body';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['author']){
        $data['value_content'] = $node['author'];
        $data['type_content'] = 'author';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['type_art']){
        $data['value_content'] = $node['type_art'];
        $data['type_content'] = 'type_art';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['start_time']){
        $data['value_content'] = $node['start_time'];
        $data['type_content'] = 'start_time';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['end_time']){
        $data['value_content'] = $node['end_time'];
        $data['type_content'] = 'end_time';
        if (!drupal_write_record('mailcontent_content', $data)){
            return false;
        }
    }
    if ($node['inquiry_question']){
        $data['type_content'] = 'inquiry_question';
        foreach ($node['inquiry_question'] as $value) {
            $data['value_content'] = $value;
            if (!drupal_write_record('mailcontent_content', $data)){
                return false;
            }
        }
    }
    if ($node['block']){
        $data['type_content'] = 'block';
        foreach ($node['block'] as $value) {
            $data['block'] = $value;
            if (!drupal_write_record('mailcontent_content', $data)){
                return false;
            }
        }
    }

    return true;
} //OK

/*
 * Funkce vrací hodnotu path z tabulky akcí nebo false, pokud pro dotazovanou akci path není
 */
function mailcontent_db_have_path($action){        
drupal_set_message("SELECT path FROM {mailcontent_actions} WHERE name='".$action."'");
    if (db_result(db_query("SELECT path FROM {mailcontent_actions} WHERE name='".$action."'"))){
        $result = db_query("SELECT path FROM {mailcontent_actions} WHERE name='".$action."'");
        drupal_set_message($row['path']);
        return $row['path'];
    } else {
        drupal_set_message("$have path - end");
        return false;
    }
} //OK

/*
 * Funkce přidává do systému nový článek
 */
function mailcontent_db_add_node($data) {

    global $user;
    if (user_access('content edit')) {
    $types = node_get_types();
    if (!isset($types[$data['type_art']])){
        $data['type_art'] = 'page';
    }

    if (!$data['title'] || !$data['teaser'] || !$data['body'] || !$data['type_art'] || !$data['received']){
        return false;
    }

    $node = new stdClass();
// Your script will probably pull this information from a database.
    $node->title = $data['title'];
    $node->teaser = $data['teaser'];
    $node->body = $data['body'];
    $node->type = $data['type_art'];   // Your specified content type
    $node->created = strtotime($data['received']);
    $node->changed = $node->created;
    $node->status = 1;
    $node->promote = 1;
    $node->sticky = 0;
    $node->format = 2;
// $node-> $user->uid;          // UID of content owner
    $node->language = '';
// If known, the taxonomy TID values can be added as an array.
//$node->taxonomy = array(2,3,1,);
    node_save($node);
    return db_last_insert_id('node', 'nid');
        } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK

/*
 * Funkce přidává log do databáze
 */
function mailcontent_db_add_log($text, $type){
    $data = array(
        "time_log" => date("Y-m-d H:i:s"),
        "value_log" => $text,
        "type_log" => $type
    );
    drupal_write_record('mailcontent_log', $data);
} //OK

/*
 * Funkce upravuje status mailu - publikováno/nepublikováno
 */
function mailcontent_db_update_mail_status($idm = 0, $idn = 0){
    global $user;
    if (user_access('content user')) {
    if ($idm){
        if ($idn == 0){
            $status = 0;
        } else {
            $status = 1;
        }
        if (db_query("UPDATE {mailcontent_mail} SET idn=".$idn.", state=".$status." WHERE idm=".$idm)){
            mailcontent_info(t('Mail (#'.$idm.t(") was updated.")));
            return true;
        } else {
            mailcontent_error(t("Mail (#".$idm.") can not be updated!"));
            return false;
        }
    } else {
        mailcontent_error(t("Bad # of mail!"));
        return false;
    }
        } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK

?>
